package ar.org.sstuyv

import grails.converters.JSON

import org.springframework.dao.DataIntegrityViolationException

class PersonalController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [personalInstanceList: Personal.list(params), personalInstanceTotal: Personal.count()]
    }

    def create() {
        [personalInstance: new Personal(params)]
    }

    def save() {
        def personalInstance = new Personal(params)
        if (!personalInstance.save(flush: true)) {
            render(view: "create", model: [personalInstance: personalInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'personal.label', default: 'Personal'), personalInstance.id])
        redirect(action: "show", id: personalInstance.id)
    }

    def show(Long id) {
        def personalInstance = Personal.get(id)
        if (!personalInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'personal.label', default: 'Personal'), id])
            redirect(action: "list")
            return
        }

        [personalInstance: personalInstance]
    }

    def edit(Long id) {
        def personalInstance = Personal.get(id)
        if (!personalInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'personal.label', default: 'Personal'), id])
            redirect(action: "list")
            return
        }

        [personalInstance: personalInstance]
    }

    def update(Long id, Long version) {
        def personalInstance = Personal.get(id)
        if (!personalInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'personal.label', default: 'Personal'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (personalInstance.version > version) {
                personalInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'personal.label', default: 'Personal')] as Object[],
                          "Another user has updated this Personal while you were editing")
                render(view: "edit", model: [personalInstance: personalInstance])
                return
            }
        }

        personalInstance.properties = params

        if (!personalInstance.save(flush: true)) {
            render(view: "edit", model: [personalInstance: personalInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'personal.label', default: 'Personal'), personalInstance.id])
        redirect(action: "show", id: personalInstance.id)
    }

    def delete(Long id) {
        def personalInstance = Personal.get(id)
        if (!personalInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'personal.label', default: 'Personal'), id])
            redirect(action: "list")
            return
        }

        try {
            personalInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'personal.label', default: 'Personal'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'personal.label', default: 'Personal'), id])
            redirect(action: "show", id: id)
        }
    }
	
	def listJSON = {
		
		def sortIndex = params.sidx ?: 'apellido'
		def sortOrder  = params.sord ?: 'desc'
		def maxRows = Integer.valueOf(params.rows)
		def currentPage = Integer.valueOf(params.page) ?: 1
		def rowOffset = currentPage == 1 ? 0 : (currentPage - 1) * maxRows
		
		def personal = Personal.createCriteria().list(max: maxRows, offset: rowOffset) {
//			if (params.id) eq('id', params.id.toLong())
			order(sortIndex, sortOrder)
		}
		
		def totalRows = personal.totalCount
		def numberOfPages = Math.ceil(totalRows / maxRows)
		
		def results = personal?.collect {
				[
					cell: [it.id, it.apellido, it.nombre, it.departamento, it.direccion, it.legajo],
					id: it.id
				]
		}

		
//		def jsonData = [rows: results, page: currentPage, records: totalRows, total: numberOfPages ]
		def jsonData = ['']
		render jsonData as JSON
	
	}
}
